#!/bin/bash
#
# htlibr - bash wrapper around CP/M's LIBR

VERSION=0.2
LAST_UPDATE="02 June 2002"

#############################################################################
#
# Some points of consideration:
#
# - you need to have cpmemu installed
#
# - the path to HiTech C's .com files has to be fully lowercase
#   (restriction of cpmemu :-/) 
#
# - For htlibr, HiTech C is expected to be installed as follows:
#
#    ...$HTC_PATH/         - HiTech C base dir
#                 bin      - CPM binary (libr)
#
# - the X key doesn't function properly, yet! Extracting single objects
#   from a library should work, but extracting *all* objects (without
#   object specification) doesn't
#
#############################################################################

WORKDIR=/tmp/htlibr
CURDIR=`pwd`

#############################################################################
#
# Usage
#

Usage ()
{
  cat <<-END
	htlibr - z80 C library utility using HiTech's LIBR and cpmemu
	version $VERSION - $LAST_UPDATE - Aurora (Eric Boon & Manuel Bilderbeek)

	Usage: htlibr [options] k library object ...

	OPTIONS:
	  -b      Keep backup of original library
	  -v      Be verbose
	  -h      Print this help

	The k(ey) letters are:

	  r       replace modules
	  d       delete modules
	  x       extract modules
	  m       list module names
	  s       list modules with symbols

	ENVIRONMENT VARIABLES:
	  HTC_PATH          Path to HiTech C install
	                    (/usr/local/lib/hitech)
	  HTC_CPMEMU        Path to 'cpm', the CP/M emulator
	                    (/usr/local/bin/cpm)

	TIPS:

	- Don't use 'x' without specifying any object file. It won't work

	DISCLAIMER:
	  It works for us, there's no guarantee that it will for you! :-)
	  Nevertheless, please send bug reports, questions, remarks,
	  thank-you's or beer to: Aurora1@Chello.nl

	COPYRIGHT:
	  Find yourself a copy of the GNU Public License.
	END

  exit 1
}


#############################################################################
#
# Try to find the CPM emulator
#

Check_cpmemu ()
{
  CPMEMU_LIST="$HTC_CPMEMU `which cpm` /usr/local/bin/cpm "

  for MY_CPM in $CPMEMU_LIST ; do
    if [ ! -x $MY_CPM ] ; then break ; fi
  done

  if [ -z $MY_CPM ] ; then
    echo "* $ME: Unable to find cpm emulator" \
         "Please set \$HTC_CPMEMU appropriately"
    exit 1
  fi
}


#############################################################################
#
# Try to find LIBR
#

Check_libr ()
{
  MY_HTC_PATH=$HTC_PATH
  if [ -z $MY_HTC_PATH ] ; then
    MY_HTC_PATH="/usr/local/lib/hitech"
  fi

  if [ ! -d $MY_HTC_PATH ] ; then
    echo "* $ME: Unable to find HiTech C" \
         "Please set \$HTC_PATH appropriately"
    exit 1
  fi

  if [ $VERBOSE == 1 ] ; then
    echo "HITECH PATH    : $MY_HTC_PATH"
  fi

  LIBR=$MY_HTC_PATH/bin/libr.com

  if [[ ! ( -e $LIBR ) ]] ; then
    echo "* $ME: Unable to find LIBR! Oops..."
    exit 1
  fi
}

#############################################################################
#
# Make Library
#

do_libr ()
{
  if [ -z "$MY_OBJECTS" ] ; then
    $MY_CPM -f -x $LIBR $KEY $MY_LIB
  else	
    for OBJECT in $MY_OBJECTS ; do
      $MY_CPM -f -x $LIBR $KEY $MY_LIB $OBJECT
    done
  fi
}
#############################################################################
#
# Clean up and exit
#

Quit ()
{
  rm -rf /tmp/htlibr
  exit 1
}


#############################################################################
#
# M A I N
#
#############################################################################

#
# Enable extended pattern matching
#

shopt -s extglob

#
# Whoami ?
#
ME=${0/*\//}

#
# Collect options...
#

VERBOSE=0
BACKUP=0

while getopts bvh c; do
  case $c in
    b)
      BACKUP=1
      ;;
        
    v)
      VERBOSE=1
      ;;
        
    h|*)
      Usage
      ;;
  esac
done

if [ $VERBOSE == 1 ] ; then
  echo "$ME v$VERSION - $LAST_UPDATE - (C) 2002 Aurora"
fi

let CNT=$OPTIND-1 
shift $CNT
unset CNT
KEY=$1
LIBRARY=$2
shift 2
OBJECTS=( $* )

if [[ -z $KEY || -z $LIBRARY ]] ; then
  echo "* $ME: Missing key or library. (Type $ME -h to get some help.)"
  exit 1
fi

if [ $VERBOSE == 1 ] ; then
  echo "KEY    : $KEY"
  echo "LIBRARY: $LIBRARY"
  echo "OBJECTS: ${OBJECTS[*]}"
fi

#
# Check if everything is there
#

Check_cpmemu
Check_libr

#
# Ready to rock and roll...
#

#
# Create temporary working area
#

rm -rf $WORKDIR
mkdir $WORKDIR

#
# Strip path from library and copy to working area
#

MY_LIB=${LIBRARY/*\//}
LIBPATH=${LIBRARY/%${MY_LIB}/}

if [ -z $LIBPATH ] ; then
  LIBRARY=$CURDIR/$LIBRARY
fi

if [ -s $LIBRARY ] ; then
  cp $LIBRARY $WORKDIR
fi

#
# Make object list and copy object files to work dir
#

for FILE in ${OBJECTS[*]} ; do
  MY_OBJECTS="$MY_OBJECTS ${FILE/*\//}"
  if [ $KEY != "x" ] ; then
    cp $FILE $WORKDIR
  fi
done

#
# Now call libr...
#

cd $WORKDIR
do_libr

#
# Now, do smart things
#

case $KEY in 
  r|d)
   #
   # Do the really smart things.
   #
   if [ $VERBOSE == 1 ] ; then echo $KEY ; fi
   if [ ! -s $MY_LIB ] ; then
     echo "* $ME: Modified library $MY_LIB non-existent or zero size. Panic."
     Quit
   elif [ ! -s $LIBRARY ] ; then
     if [ $VERBOSE == 1 ] ; then
       echo "Moving new library to final destination"
     fi
     mv $MY_LIB $LIBRARY
   elif ! cmp -s $MY_LIB $LIBRARY ; then
     if [ $VERBOSE == 1 ] ; then
       echo "Library changed - replacing"
     fi
     if [ $BACKUP == 1 ] ; then
       if [ $VERBOSE == 1 ] ; then
         echo "Making backup of original lib"
       fi
       mv -f $LIBRARY $LIBRARY.backup
     fi
     mv -f $MY_LIB $LIBRARY
   fi
   ;;  
  x)
    for FILE in $MY_OBJECTS ; do
      if [ $VERBOSE == 1 ] ; then
        echo "Moving extracted objects to destination"
      fi
      if [ -e $FILE ] ; then 
        cp -i $FILE $CURDIR
      fi
    done
  ;;
  m|s)
  ;;
  *)
    exit 1;
esac

# clean up and exit 0

#rm -rf $WORKDIR


